<?php
include '../lib/Useradmin.php';
$userOb = new Useradmin();

//Xu ly them xoa
$command = '';
$idUsers = '';
$message = array();
if (isset($_GET['command'])) {
    $command = $_GET['command'];
}
if (isset($_GET['idUsers'])) {
    $idUsers = $_GET['idUsers'];
}
if ($command == 'del') {
    if ($userOb->xoaThanhVien($idUsers)) {
        $message[] = "Xóa thành công";
    } else {
        $message[] = "Xóa thất bại";
    }
}
//xac dinh bao nhieu dong
$display = 7;
// tinh tong so trang can hien thi
if (isset($_GET['page']) && (int) $_GET['page']) {
    $page = $_GET['page'];
} else { //neu chua xac dinh, thi tim so trang
    $record = $userOb->DemThanhVien();
    if ($record > $display) {
        $page = ceil($record / $display);
    } else {
        $page = 1;
    }
}

$start = (isset($_GET['start']) && (int) $_GET['start'] >= 0) ? $_GET['start'] : 0;
$dsThanhVien = $userOb->layDanhSachThanhVien($start, $display)
//-------------------
?>
<link href="admin.css" rel="stylesheet">
<div style="margin: 4px;padding: 30px;">
    <h2 style="color: #0000cc" align="center">Quản lý tài khoản</h2>
    <div class="messages">
        <?php foreach ($message as $mes): ?>
            <p><?php echo $mes; ?></p>
        <?php endforeach; ?>
    </div>
    <table style="width: 100%">
        <thead>
            <tr>
                <th scope="col" align="left">ID</th>
                <th scope="col" align="left">Tên tài khoản</th>
                <th scope="col" align="left">Mật khẩu</th>
                <th scope="col" align="left">Họ</th>
                <th scope="col" align="left">Tên</th>
                <th scope="col" align="left">Email</th>
                <th scope="col" align="left">Giới tính</th>
                <th scope="col" align="left">Ngày sinh</th>
                <th scope="col" align="left">Địa chỉ</th>
                <th scope="col" align="left">Nơi ở</th>
                <th scope="col" align="left">Điện thoại</th>
                <th scope="col" align="left">Loai User</th>
                <th scope="col"></th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($dsThanhVien as $thanhvien): ?>
                <tr>
                    <td scope="col"><?php echo $thanhvien['idUsers'] ?></td>
                    <td scope="col"><?php echo $thanhvien['UserName'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Pass'] ?></td>
                    <td scope="col"><?php echo $thanhvien['LastName'] ?></td>
                    <td scope="col"><?php echo $thanhvien['FirstName'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Email'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Sex'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Birthday'] ?></td>
                    <td scope="col"><?php echo $thanhvien['DiaChi'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Location'] ?></td>
                    <td scope="col"><?php echo $thanhvien['PhoneNum'] ?></td>
                    <td scope="col"><?php echo $thanhvien['Loai_User'] ?></td>
                    <td scope="col"><?php echo "<a href='/admin/suathanhvien.php?command=edit&idUsers=" . $thanhvien['idUsers'] . "'>Sửa"; ?></td>
                    <td scope="col"><?php echo "<a href='/admin/index.php?command=del&idUsers=" . $thanhvien['idUsers'] . "'>Xóa"; ?></td>                   
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <th scope="row" colspan="14"><p style="color: #0099ff">.:ADMIN quản lý tài khoản:.</p></th>
        </tr>
        </tfoot>
    </table>
     <div style="padding: 0 auto;" align="center"><?php
            if ($page > 1) { //neu can hien thi so trang
                $next = $start + $display;
                $prev = $start - $display;
                $current = ($start / $display) + 1;

                //Hien thi trang Previous
                if ($current != 1) {
                    echo "<a href='index.php?start=$prev&page=$page'>Previous</a>";
                    echo ".";
                }
                //Hien thi so link
                for ($i = 1; $i <= $page; $i++) {
                    if ($current != $i) {
                        echo ".";
                        echo "<a href='index.php?start=" . ($display * ($i - 1)) . "&page=$page'>$i</a>";
                    } else {
                        echo $i;
                    }
                }
                //Hien thi trang Next
                if ($current != $page) {
                    echo "..";
                    echo "<a href='index.php?start=$next&page=$page'>Next</a>";
                }
            }//End: $page > 1 IF
            ?>
     </div>
</div>